package com.cari.oa.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.OperationLogEntity;
import com.cari.oa.util.StrUtils;

@Repository
public class OperationLogDao extends BaseDao<OperationLogEntity>{

	/**
     * 根据ID获取实体
     * @param Id
     * @return 对象
     */
    public OperationLogEntity getById(String id) {
        Query query = getSession().createQuery("from OperationLogEntity where id = :id");
        query.setString("id", id);
        return (OperationLogEntity)query.uniqueResult();
    }
    
    /**
     * 获取有记录的运维人员
     * @param Id
     * @return 对象
     */
    @SuppressWarnings("unchecked")
	public List<Map<String, String>> getOperationUser() {
    	StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select new Map(c.operationUserEn as operationUserEn, max(c.operationUserCn) as operationUserCn) ");
        stringBuilder.append("from OperationLogEntity c where 1 = 1 group by c.operationUserEn");
    	
    	Query query = getSession().createQuery(stringBuilder.toString());
        return (List<Map<String, String>>)query.list();
    }
    
    /**
     * 判断该英文名实体对象是否存在
     * @param enname 系统字典项英文名
     * @return 是否存在
     */
    public boolean existByTitle(String title) {
        Query query = getSession().createQuery("select count(*) from OperationLogEntity where title = :title");
        query.setString("title", title);
        Long count = (Long)query.uniqueResult();
        return count > 0;
    }
    
    /**
     * 分页查询系统字典项实体对象
     * @param title 系统字典项英文名
     * @param page 当前页
     * @param size 每页条数
     * @return 分页实体
     * @modify by liuyoujie
     * @modTime 2013-06-21
     * 修改内容 添加按时间查询功能
     * 
     * @modify by liuyoujie
     * @modTime 2013-09-13
     * 修改内容  添加按照内容查询
     */
    public List<OperationLogEntity> getPage(String title, String content, String operationUserEn, 
    		Timestamp startTime, Timestamp endTime,
    		int page, int size) {
    	List<Object> params = new ArrayList<Object>();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("from OperationLogEntity i where 1=1 ");

        /*
         * 这部分为新增按时间查询的代码
         * ----BEGIN----
         */
        if(startTime != null){
        	stringBuilder.append(" and i.createTime > ? ");
        	params.add(startTime);
        }
        if(endTime != null){
        	stringBuilder.append(" and i.createTime < ? ");
        	params.add(endTime);
        }
        
        /*
         * ----END----
         */
        
        /**
         * 按内容查询
         * ---- BEGIN ----
         */
        if(StrUtils.isNotEmpty(content)){
        	stringBuilder.append(" and i.content like ? ");
        	params.add("%" + content + "%");
        }
        /**
         * 按内容查询
         * ---- END ----
         */
        
        if(StrUtils.isNotEmpty(title)) {
            stringBuilder.append("and i.title like ? ");
            params.add("%" + title + "%");
        }
        if(StrUtils.isNotEmpty(operationUserEn)) {
            stringBuilder.append("and i.operationUserEn like ? ");
            params.add("%" + operationUserEn + "%");
        }
        stringBuilder.append("order by i.createTime desc");
        
        return getPage(stringBuilder.toString(), page, size, params.toArray());
    }
    
    /**
     * 查询的记录数
     * @param title 系统字典项英文名
     * @return 记录数
     * @modify by liuyoujie
     * @modTime 2013-06-21
     * 修改内容 添加按时间查询功能
     * 
     * @modify by liuyoujie
     * @modTime 2013-09-13
     * 修改内容  添加按照内容查询
     */
    public long getCount(String title, String content, String operationUserEn,
    		Timestamp startTime, Timestamp endTime) {
    	List<Object> params = new ArrayList<Object>();

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select count(*) from OperationLogEntity i where 1=1 ");
        
        /*
         * 这部分为新增按时间查询的代码
         * ----BEGIN----
         */
        if(startTime != null){
        	stringBuilder.append(" and i.createTime > ? ");
        	params.add(startTime);
        }
        if(endTime != null){
        	stringBuilder.append(" and i.createTime < ? ");
        	params.add(endTime);
        }
        
        /*
         * ----END----
         */
        if(StrUtils.isNotEmpty(title)) {
            stringBuilder.append("and i.title like ? ");
            params.add("%" + title + "%");
        }
        
        /**
         * 按内容查询
         * ---- BEGIN ----
         */
        if(StrUtils.isNotEmpty(content)){
        	stringBuilder.append(" and i.content like ? ");
        	params.add("%" + content + "%");
        }
        /**
         * 按内容查询
         * ---- END ----
         */
        if(StrUtils.isNotEmpty(operationUserEn)) {
            stringBuilder.append("and i.operationUserEn like ? ");
            params.add("%" + operationUserEn + "%");
        }

        Query query = getSession().createQuery(stringBuilder.toString());
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i, params.get(i));
        }
        return (Long)query.uniqueResult();
    }
}
